<template>
  <div class="navbar">
    <hamburger class="hamburger-container" @toggleClick="toggleSideBar" />

    <!-- <breadcrumb class="breadcrumb-container" /> -->

    <div class="right-menu">
      <div v-if="unmannedSalesWorkbenchPermission" class="function-button" @click="openPage('workbenchUnmannedSales')">
        无人销售监控台</div>
      <div class="avatar-wrapper">
        <img v-if="undefined === userInfo.photoDownloadCode || '' === userInfo.photoDownloadCode"
          src="@/assets/images/default_head.png" class="user-avatar">
        <image-show v-if="undefined !== userInfo.photoDownloadCode && '' !== userInfo.photoDownloadCode"
          :md5="userInfo.photoDownloadCode" :prveiew-visible="false" class="user-avatar"
          img-style="border-radius: 50% !important;" />
        <el-dropdown ref="userInfo" class="avatar-container" trigger="click">
          <div>
            <span class="user-name">{{ userInfo.userName }}</span>
            <span class="organization-name">{{ userInfo.organizationName }}</span>
            <i class="el-icon-caret-bottom" />
          </div>
          <el-dropdown-menu slot="dropdown" class="user-dropdown">
            <div class="store-info-container">
              <div class="store-info-item" @dblclick="test()">账号：{{ userInfo.loginAccount }}</div>
              <div class="store-info-item">机构名称：{{ userInfo.organizationName }}</div>
              <div class="store-info-item" @dblclick="showId">更多：
                <el-button type="warning" size="small" style="width: 80px" @click.stop="viewUserInfo">查看</el-button>
              </div>
            </div>
            <el-dropdown-item divided @click.native="logout">
              <span style="display:block;text-align: center">安全登出</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import imageShow from '@/components/imageShow/index'
// import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import permissionMixin from '@/mixins/modules/permission'
import store from '@/store'

export default {
  components: {
    // Breadcrumb,
    Hamburger,
    imageShow
  },
  mixins: [permissionMixin],
  computed: {
    ...mapGetters([
      'sidebar',
      'userInfo'
    ])
  },
  created() {
    // console.log(223344, this.$api.getDownloadServiceUrl(this.userInfo.photoDownloadCode))
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
      window.location.reload()
    },
    showId() {
      let text = '用户编号：' + this.userInfo.userId
      text += '<br />机构编号：' + this.userInfo.organizationId
      if (this.userInfo.organizationType === 5 || this.userInfo.organizationType === 6) {
        text += '<br />连锁机构编号：' + this.userInfo.chainOrganizationId
      }
      if (this.userInfo.organizationType === 4 || this.userInfo.organizationType === 5 || this.userInfo.organizationType === 6) {
        text += '<br />总部机构编号：' + this.userInfo.headquartersOrganizationId
      }
      this.$commonUtils.showInfoAlert(text, undefined, false, true)
    },
    test() {
      // store.dispatch('updateRouterMessage', {action: 'hasNewVersion'});
    },
    viewUserInfo() {
      this.$refs.userInfo.hide()
      this.$EventBus.$emit('addTabItem', { name: 'userInfo' })
    },
    openPage(pagePath) {
      if (pagePath === 'workbenchUnmannedSales') {
        window.open('/#/workbench/unmannedSales')
        return
      }
      this.$addTab(pagePath)
    }
  }
}
</script>

<style lang="scss" scoped>
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, .08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }

    .function-button {
      height: 30px;
      line-height: 30px;
      padding: 0 10px;
      border-radius: 5px;
      color: #fff;
      background-color: #f07509;
      margin-right: 10px;
      cursor: pointer;

      &:hover {
        color: #fff;
        background-color: #a74e00;
      }
    }

    .avatar-wrapper {
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;

      .user-avatar {
        cursor: pointer;
        width: 30px;
        height: 30px;
        margin-right: 5px;
        border-radius: 50%;
        border: 1px #eee solid;
      }

      .avatar-container {
        margin-right: 30px;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;

        .user-name {
          margin-right: 5px;
        }

        .organization-name {
          color: #aaa;
        }

        .el-icon-caret-bottom {
          position: absolute;
          right: -15px;
          top: 18px;
          font-size: 12px;
        }
      }
    }
  }
}

.store-info-container {
  padding: 0 20px;
  color: #606266;

  .store-info-item {
    margin-bottom: 10px;
    font-size: 14px;
  }
}
</style>
